/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */
package acid.parse;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Parse microarray results in the format:
 * ID	$$$$G3/wt	P-value
 *
 * Where $$$$ is the organ, ID is the gene name(s), and P-value is the p value.
 * Different IDs are separated by triple forward slashes (///).
 * 
 * @author morgan
 */
public class ParseRudolphResults
{

    private ParseRudolphResults()
    {
    }

    public static List<GANRow> parse(String filename) throws IOException
    {
        List<GANRow> rows = new ArrayList<GANRow>();

        BufferedReader reader = new BufferedReader(new FileReader(filename));
        int counter = 0;
        String line = reader.readLine();
        if (!line.contains("G3/wt"))
        {
            throw new IOException("Incorrect file format");
        }
        line = reader.readLine();
        while (line != null)
        {
            if (!line.trim().equals(""))
            {
                String[] splitLine = line.split("\\t");
                String geneName = splitLine[0];
                Double foldChange = new Double(splitLine[1]);
                Double pVal = new Double(splitLine[2]);
                if (geneName.equals("---"))
                {
                    line = reader.readLine();
                    continue;
                }
                String[] names = geneName.split("///");
                for (String name : names)
                {
                    rows.add(new GANRow(name, pVal, foldChange));
                }
                line = reader.readLine();
                //System.out.println(counter++);
            }
        }

        return rows;
    }
}
